package pack;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.faces.application.ConfigurableNavigationHandler;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;

import dao.CarteDAORemote;
import dao.EmployeeDAORemote;
import dao.ListaDAORemote;
import dto.CarteDTO;
import dto.EmployeeDTO;
import dto.ListaDTO;

@ManagedBean(name = "listaBean")
@SessionScoped
public class ListaBean implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	EmailBean email = new EmailBean();
	
	@EJB
	ListaDAORemote listaDAO;
	
	@EJB
	CarteDAORemote carteDAO;
	
	@EJB
	EmployeeDAORemote employeeDAO;
	
	@ManagedProperty(value = "#{listaDTO}")
	private ListaDTO listaDTO;
	
	ExternalContext exCon;
	private int id;
	private int id_angajat;
	private int id_carte;
	private String data;
	
	List<ListaDTO> lista;
	
	public void verify(){
		FacesContext fc = FacesContext.getCurrentInstance();
		EmployeeDTO eDTO = (EmployeeDTO)fc.getExternalContext().getSessionMap().get("loggedInUser");
		
		ConfigurableNavigationHandler nav 
			= (ConfigurableNavigationHandler) 
		fc.getApplication().getNavigationHandler();
		
		if (eDTO != null){
			if (eDTO.getRank() != 5){
				nav.performNavigation("home.xhtml");
			}
		}
		else {
			nav.performNavigation("home.xhtml");
		}
		//read();
	}
	
	
	public void verifyA(){
		FacesContext fc = FacesContext.getCurrentInstance();
		EmployeeDTO eDTO = (EmployeeDTO)fc.getExternalContext().getSessionMap().get("loggedInUser");
		
		ConfigurableNavigationHandler nav 
			= (ConfigurableNavigationHandler) 
		fc.getApplication().getNavigationHandler();
		
		if (eDTO != null){
			if (eDTO.getRank() != 1){
				nav.performNavigation("home.xhtml");
			}
		}
		else {
			nav.performNavigation("home.xhtml");
		}
		read();
	}
	
	public String read(){
		lista = listaDAO.findAllL();
		this.setLista(lista);
		
		return "";
	}
	
	public String accept(ListaDTO lista){
		listaDAO.accept(lista);
		EmployeeDTO employee = new EmployeeDTO();
		employee = employeeDAO.getEmployeeById(lista.getId_angajat());
		String to = employee.getEmail();
		String from = employeeDAO.findAdminEmail();
		String subject = "Book request!";
		String msg = "Hi " + employee.getFirstname() + "!\n\n" 
				+ "Your request for the book: "
				+ lista.getTitlu() + " was accepted.\n\n";
		email.sendMail(from, to, subject, msg);
		return "";
	}
	
	public String refuza(ListaDTO lista){
		listaDAO.refuza(lista);
		EmployeeDTO employee = new EmployeeDTO();
		employee = employeeDAO.getEmployeeById(lista.getId_angajat());
		String to = employee.getEmail();
		String from = employeeDAO.findAdminEmail();
		String subject = "Book request!";
		String msg = "Hi " + employee.getFirstname() + "!\n\n" 
				+ "Your request for the book: "
				+ lista.getTitlu() + " was refused.\n\n";
		email.sendMail(from, to, subject, msg);
		return "";
	}
	
	public String toLista(CarteDTO carte){
		List<ListaDTO> lista2 = new ArrayList<ListaDTO>();
		lista2 = listaDAO.getListaByIdCarte(carte.getId());
		for (int i = 0; i<lista2.size(); i++){
			String d = "";
			for (int j = 0; j<10; j++){
				d = d + lista2.get(i).getData().charAt(j);
			}
			lista2.get(i).setData(d);
		}
		this.setLista(lista2);
		
		return "listaAngajati.xhtml";
	}
	
	public ListaDTO getListaDTO() {
		return listaDTO;
	}
	public void setListaDTO(ListaDTO listaDTO) {
		this.listaDTO = listaDTO;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public int getId_angajat() {
		return id_angajat;
	}
	public void setId_angajat(int id_angajat) {
		this.id_angajat = id_angajat;
	}
	public int getId_carte() {
		return id_carte;
	}
	public void setId_carte(int id_carte) {
		this.id_carte = id_carte;
	}
	public String getData() {
		return data;
	}
	public void setData(String data) {
		this.data = data;
	}

	public List<ListaDTO> getLista() {
		return lista;
	}

	public void setLista(List<ListaDTO> lista) {
		this.lista = lista;
	}

}
